package org.daidai.abstrusewriting.homepage.hot.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import org.daidai.abstrusewriting.R;
import org.daidai.abstrusewriting.homepage.hot.model.ProgremTracks;
import org.daidai.abstrusewriting.homepage.hot.model.RecommendItemDetail;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/11/3.
 */

public class MyProgremAdapter extends BaseAdapter{
    private List<ProgremTracks.TrackItem> mList;
    private Context mContext;

    public MyProgremAdapter(Context context, List<ProgremTracks.TrackItem> list) {
        mContext = context;
        mList = list;
    }

    @Override
    public int getCount() {
        int ret = 0;
        if (mList!=null){
            ret = mList.size();
        }
        return ret;
    }

    @Override
    public ProgremTracks.TrackItem getItem(int position) {
        ProgremTracks.TrackItem ret = null;
        if (mList!=null) {
            ret = mList.get(position);
        }
        return  ret;

    }

    @Override
    public long getItemId(int position) {
        long ret = 0;
        if (mList!=null) {
            ret = mList.get(position).getAlbumId();
        }
        return ret;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView==null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.hotfragment_recommend_detail_progrem,parent,false);
            convertView.setTag(new ViewHolder(convertView));
        }
        ProgremTracks.TrackItem trackItem = mList.get(position);
        ViewHolder holder = (ViewHolder) convertView.getTag();
        holder.mtitle.setText(trackItem.getTitle());
        holder.mplaynumber.setText(trackItem.getPlaytimes());
        holder.mcommoncount.setText(String.valueOf(trackItem.getComments()));
        holder.mtimelength.setText(trackItem.getDuration());
        Picasso.with(mContext).load(trackItem.getSmallLogo())
                .config(Bitmap.Config.ARGB_8888).into(holder.mImageView);
        long createdAt = trackItem.getCreatedAt();
        SimpleDateFormat format = new SimpleDateFormat("dd");
            String s = format.format(createdAt);
            holder.mUpdatetime.setText(s+"天前更新");
        return convertView;
    }

    public static class ViewHolder{
        private TextView mtitle;
        private TextView mcommoncount;
        private TextView mplaynumber;
        private TextView mtimelength;
        private ImageView mImageView;
        private TextView mUpdatetime;
        public ViewHolder(View view){
            mtitle=((TextView) view.findViewById(R.id.fragment_program_listitem_title));
            mplaynumber=((TextView) view.findViewById(R.id.fragment_program_listitem_playnumber));
            mcommoncount=((TextView) view.findViewById(R.id.fragment_program_listitem_commoncount));
            mtimelength=((TextView) view.findViewById(R.id.fragment_program_listitem_timelength));
            mImageView=((ImageView) view.findViewById(R.id.fragment_program_listitem_imageview));
            mUpdatetime=((TextView) view.findViewById(R.id.fragment_program_listitem_updatetime));
        }
    }
}
